<?php


	class FTALinks extends FTAObject {
		
		var $cmsDocument;
		var $allLinkCategories;
		var $linksCategory;
		var $categoryIdentifier;
		var $renderLinksFunction;
		
		
		function FTALinks() {
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
						
		}
		
		function &getLinkCategories(){
			if (is_null($this->allLinkCategories)){
			
				$this->cmsDocument->flush();
				$this->cmsDocument->setActiveDocuments();
				$this->cmsDocument->addDocumentType('docLinkCat');
				$this->cmsDocument->addSort('<<md.rank>>');
				$this->cmsDocument->resetIterator();
			
				$this->allLinkCategories = $this->cmsDocument->getAllDocuments();
				
				if (!is_array($this->allLinkCategories))
					$this->allLinkCategories = array();

			}
			
			return $this->allLinkCategories;
		}


//
		
		function getLinksCategory($categoryIdentifier){
			
			$this->categoryIdentifier = $categoryIdentifier;
			$allLinkCategories = &$this->getLinkCategories();
			$filteredLinkCategory = array_filter($allLinkCategories, array($this, 'filter_linksCategoryByCategoryId'));
			
			if (1 == count($filteredLinkCategory)) {
				$this->linksCategory = array_pop($filteredLinkCategory);
				return true;
			} else {
				return false;
			}
		}
		
		function filter_linksCategoryByCategoryId($o){
			if (is_null($this->categoryIdentifier)) {
				return false;
			} else if (is_numeric($this->categoryIdentifier)){
				return $this->categoryIdentifier == $o->documentcode;
			} else {
				return $this->categoryIdentifier == $o->title;
			}
		}
		
//		
		function renderLinksCategoryNavigation(){
			$allLinkCategories = &$this->getLinkCategories();
			$allLinkCategories = array_map(array($this,'map_linksCategoryCmsDocumentRecordToLinkList'),$allLinkCategories);
			fta_widgetNavigationList($allLinkCategories,'links');
		}
		
		function map_linksCategoryCmsDocumentRecordToLinkList($o){
			global $fta_;
		
			$oo = new stdClass();
			$oo->destination = $fta_->getPageScope() . urlFragmentClean($o->title) . "/";//$o->urlfragment;
			$oo->text = $o->title;

			return $oo;			
		}

//		
		function getLinks($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){
			//this gets links and filters based on the category if there is one set.. 
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docLink');
						
			if(!is_null($category)){
				$this->cmsDocument->addConstraint("categorycode", $category);
			} elseif($this->linksCategory){
				$this->cmsDocument->addConstraint('categorycode',$this->linksCategory->documentcode,'=');
			}
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("<<md.rank>>");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;
						
		}
		
// render methods
		
		function renderLinksCategoryAsTagLine(){	

			if(is_null($this->linksCategory))
				return;				
			
			echo fta_widgetH5Tag($this->linksCategory->title);					
			
		}
		
		function renderLinksCategoryAsSubHeading(){	
			
			if(is_null($this->linksCategory))
				return;				
			
			echo fta_widgetH2Tag($this->linksCategory->title);		
							
		}
		
		function renderLinksCategoryAsMinorSubHeading(){
			if(is_null($this->linksCategory))
				return;				
			
			echo fta_widgetH3Tag($this->linksCategory->title);				
		}
		
		function renderLinksCateogryAsHeading(){	
			if(is_null($this->linksCategory))
				return;				
			
			echo fta_widgetH1Tag($this->linksCategory->title);							
		}
		
		function renderLinksCateogryDescription(){
			if(is_null($this->linksCategory))
				return;				
			
			echo $this->linksCategory->shortdescription;			
		}
		
		
		function renderLinks($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){ 
		
			$links = $this->getLinks($pageSize, $pageNumber, $category, $sort, $direction);
		
			if (0 == count($links))	return;
			
			if(is_null($this->renderLinksFunction)) {
				global $FTALinkItemRenderFunction;
				if(!isset($FTALinkItemRenderFunction)){
					$FTALinkItemRenderFunction = '$o=fta_getSharedLinksObject(); echo $o->renderLinksItemFunction($item);';
				}
	
				$this->renderLinksFunction  = create_function('$item', $FTALinkItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($links,"linksList","altItem","firstItem", "lastItem",$this->renderLinksFunction);
		}
		
		function renderLinksItemFunction($item){
		//	//this is a fucking prick job.. screw over.. 
			if ($item->content) {
				$imageUrl  = "/html/blob.php?documentCode=" . $item->documentcode . "&elementId=20064&filetypecode=2";
?>
<div class="linksThumb"><a <?= ($item->authordepartment? "target=\"_blank\"" : "")?> href="<?= $item->author ?>"><img src="<?=$imageUrl ?>" alt="<?= $item->title ?>"/></a></div>
<?
			}
?>
<div class="linksDetail"><a <?= ($item->authordepartment? "target=\"_blank\"" : "")?> href="<?= $item->author ?>"><?= $item->title ?></a><br />
<span class="linksListSub"><?= $item->shortdescription ?></span></div>
<?
		}
	}
	
/////

	function fta_getLinksCategoryIdentiferFromRequestOrDefault($defaultLinksCategory=null){
		$categoryIdentifier = extractStringWithKeyFromArray('REQUEST_URI',$_SERVER,null);
		
		// we need to subtract the page scope from the passed in url
		global $fta_;
		$pageScope = $fta_->getPageScope();
		//match the string ... 
		//scope/xxx/yyy/
		if( preg_match("#^".$pageScope."(.*)/$#",$categoryIdentifier,$matches) ){
			$categoryIdentifier = urlFragmentCleanRestore($matches[1]);
		} else {
			$categoryIdentifier = $defaultLinksCategory;
		}

		//actually we will just pull it from the request.. 
		
		
		$linksObject = &fta_getSharedLinksObject();
		if (!is_null($categoryIdentifier) && $linksObject->getLinksCategory( $categoryIdentifier )){
			return $categoryIdentifier;
		} else if (!is_null($defaultLinksCategory) && $linksObject->getLinksCategory( $defaultLinksCategory )){
			return $defaultLinksCategory;
		} else {
			return null;
		}
	}
	
	function fta_getLinksCategoryNavigation(){
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCategoryNavigation();
		return ob_get_clean();
	}
	
	function fta_links() {
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinks();
		return ob_get_clean();		
	}
	
	
	function fta_getLinksCategoryAsTagLine(){
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCategoryAsTagLine();
		return ob_get_clean();			
	}
	
	function fta_getLinksCategoryAsSubHeading(){
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCategoryAsSubHeading();
		return ob_get_clean();			
	}
	
	
	function fta_getLinksCategoryAsMinorHeading(){
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCategoryAsMinorSubHeading();
		return ob_get_clean();			
	}	

	function fta_getLinksCateogryAsHeading(){
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCateogryAsHeading();
		return ob_get_clean();			
	}
	
	function fta_getLinksCategoryDescription() {
		ob_start();
		$linksObject = &fta_getSharedLinksObject();
		$linksObject->renderLinksCateogryDescription();
		return ob_get_clean();			
	}
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedLinksObject(){

		global $fta_;

		global $FTALinksClass;
		
		if(!is_null($FTALinksClass) && "FTALinks" != $FTALinksClass){
		} else {
			$FTALinksClass = "FTALinks";
		}
			
		if (!array_key_exists('ftaLinks',$fta_)){
			$fta_->ftaLinks = new  $FTALinksClass();
		}
		return $fta_->ftaLinks;
	}